home *** CD-ROM | disk | FTP | other *** search
- ' This web service contains a number of do-nothing methods
- ' whose purpose is to test advanced web service techniques,
- ' such as caching and soap extensions.
-
- Imports System.Web.Services
- Imports System.Web.Services.Protocols
- Imports System.Net
- Imports System.Threading
- Imports System.Web.Security
- Imports System.Xml.Serialization
-
- <WebService(Description:="A web service with methods for doing tests", _
- Namespace:="http://www.vb2themax.com/")> _
- Public Class SampleService
- Inherits System.Web.Services.WebService
-
- #Region " Web Services Designer Generated Code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Web Services Designer.
- InitializeComponent()
-
- 'Add your own initialization code after the InitializeComponent() call
-
- End Sub
-
- 'Required by the Web Services Designer
- Private components As System.ComponentModel.IContainer
-
- 'NOTE: The following procedure is required by the Web Services Designer
- 'It can be modified using the Web Services Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- components = New System.ComponentModel.Container()
- End Sub
-
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- 'CODEGEN: This procedure is required by the Web Services Designer
- 'Do not modify it using the code editor.
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
-
- #End Region
-
- ' This method is useful to test cache behavior.
-
- <WebMethod(CacheDuration:=10)> _
- Function GetTime(ByVal arg As Integer) As Date
- Me.Application("GetTimeCounter") = GetTimeCounter() + 1
- Return Date.Now
- End Function
-
- ' this method returns the number of times the GetTime
- ' method has actually executed, so that you can check that
- ' if the method is cached, it isn't even reached by ASP.NET
-
- <WebMethod(Description:="The number of times GetTime has been called")> _
- Function GetTimeCounter() As Integer
- Dim o As Object = Me.Application("GetTimeCounter")
- If o Is Nothing Then
- Return 0
- Else
- Return CInt(o)
- End If
- End Function
-
- ' These two methods are used to test session state persistence.
-
- <WebMethod(Enablesession:=True)> _
- Function IncrementCounter() As Integer
- Dim o As Object = Session("counter")
- If o Is Nothing Then
- Session.Add("counter", 1)
- Else
- Session("counter") = CInt(o) + 1
- End If
- Return CInt(Session("counter"))
- End Function
-
- <WebMethod(Enablesession:=True)> _
- Function GetSessionID() As String
- Return Session.SessionID
- End Function
-
- ' this method is used to simulate a length method call
-
- <WebMethod(Description:="A lengthy method")> _
- Function LengthyMethodCall(ByVal seconds As Integer) As Integer
- ' wait for the specified number of seconds.
- System.Threading.Thread.Sleep(seconds * 1000)
- ' return the argument multiplied by 1000.
- Return seconds * 1000
- End Function
-
- ' this method is marked with the OneWay attribute
-
- <WebMethod(), SoapDocumentMethod(OneWay:=True)> _
- Sub OneWayLengthyMethodCall(ByVal seconds As Integer)
- ' wait for the specified number of seconds.
- System.Threading.Thread.Sleep(seconds * 1000)
- End Sub
-
- ' this method shows how you can have a method that returns
- ' an object that can be one of many concrete classes
-
- <WebMethod(), SoapRpcMethod(), SoapInclude(GetType(Invoice)), SoapInclude(GetType(PurchaseOrder))> _
- Function GetDocument(ByVal docname As String) As Document
- Select Case docname.ToLower
- Case "invoice"
- Return New Invoice()
- Case "purchaseorder"
- Return New PurchaseOrder()
- End Select
- End Function
-
- ' this method can be used to test Soap Exceptions
-
- <WebMethod()> _
- Sub ThrowAnException()
- Throw New NullReferenceException()
- End Sub
-
- ' This is the Public variable that will receive the userInfo header.
- Public userInfo As UserInfoHeader
-
- <WebMethod(), SoapHeader("userInfo", Required:=False)> _
- Function GetClientTime() As String
- ' Provide a default userInfo object if necessary.
- If userInfo Is Nothing Then
- userInfo = New UserInfoHeader()
- End If
-
- Dim serverTime As Date = Date.Now.ToUniversalTime
- Dim clientTime As Date = serverTime.AddHours(userInfo.TimeOffset)
- ' Creates a CultureInfo object with proper locale information.
- Dim ci As New System.Globalization.CultureInfo(userInfo.Culture)
- ' Return the time formatted using client's formatting rules.
- Return clientTime.ToString(ci)
- End Function
-
- Public accountInfo As AccountInfoHeader
-
- <WebMethod(), SoapHeader("accountInfo")> _
- Function ProtectedMethod() As Boolean
- ' Check that credentials are ok, throw exception if not.
- ValidateAccount()
- ' Return a value.
- Return True
- End Function
-
- <WebMethod(), SoapHeader("accountInfo")> _
- Function AnotherProtectedMethod() As Boolean
- ' Check that credentials are ok, throw exception if not.
- ValidateAccount()
- ' Return a value.
- Return True
- End Function
-
- ' validate username and password.
- Private Sub ValidateAccount()
- ' Throw exception if missing header.
- If accountInfo Is Nothing Then
- Throw New SoapException("Missing account header", SoapException.ClientFaultCode)
- End If
-
- ' Throw exception if header members aren't set.
- If accountInfo.UserName = "" Or accountInfo.Password = "" Then
- Throw New SoapException("Missing account info", SoapException.ClientFaultCode)
- End If
-
- ' Retrieve the subscription level of this user.
- Dim thisUserSubscriptionLevel As Integer
- thisUserSubscriptionLevel = GetUserSubscriptionLevel(accountInfo.UserName, accountInfo.Password)
- ' exit if credentials were invalid.
- If thisUserSubscriptionLevel < 0 Then
- Throw New SoapException("Unknown user", SoapException.ClientFaultCode)
- End If
-
- ' Retrieve the name of the method that called this procedure.
- Dim st As New System.Diagnostics.StackTrace(False)
- Dim sf As System.Diagnostics.StackFrame = st.GetFrame(1)
- Dim mb As System.Reflection.MethodBase = sf.GetMethod
-
- ' Retrieve the required subscription level for the calling method.
- Dim requiredSubscriptionLevel As Integer
- Select Case mb.Name
- Case "ProtectedMethod"
- requiredSubscriptionLevel = 1
- Case "AnotherProtectedMethod"
- requiredSubscriptionLevel = 2
- End Select
-
- ' Throw exception if subscription level isn't sufficient.
- If thisUserSubscriptionLevel < requiredSubscriptionLevel Then
- Throw New SoapException("Insufficient subscription level", SoapException.ClientFaultCode)
- End If
- ' Exit regularly if everything is ok.
- End Sub
-
- <WebMethod(), SoapCustomAuthentication(2), SoapHeader("accountInfo")> _
- Function YetAnotherProtectedMethod() As Boolean
- ' return a dummy value in this demo
- Return True
- End Function
-
- End Class
-
- Public Class Document
- Public [Date] As Date
- Public Number As Integer
- End Class
-
- Public Class Invoice
- Inherits Document
- Public Total As Decimal
- End Class
-
- Public Class PurchaseOrder
- Inherits Document
- Public AuthorizedBy As String
- End Class
-
- Public Class UserInfoHeader
- Inherits SoapHeader
-
- ' This object contains culture info for the user.
- Public Culture As String = ""
- ' this object contains the time difference from UTC time.
- Public TimeOffset As Single = 0
-
- End Class
-
- Public Class AccountInfoHeader
- Inherits SoapHeader
-
- Public UserName As String
- Public Password As String
- End Class
-
- Module AuthenticationFunctions
-
- ' Get user subscription level, or -1 if credentials are invalid.
- Function GetUserSubscriptionLevel(ByVal username As String, ByVal password As String) As Integer
- ' (A real application would use a database instead.)
- If username = "JoeDoe" And password = "jdpwd" Then
- Return 1
- ElseIf username = "AnnSmith" And password = "aspwd" Then
- Return 2
- Else
- ' unknown user or invalid credentials
- Return -1
- End If
- End Function
- End Module
-
-